Write-Host "=== 直接修复数据库问题 ===" -ForegroundColor Green

Write-Host "`n1. 尝试直接连接到PostgreSQL数据库..." -ForegroundColor Yellow

# 检查PostgreSQL连接
$connectionString = "server=47.119.112.235;database=soft1_admin;uid=postgres;pwd=h13178502607YY;"
Write-Host "连接字符串: $connectionString" -ForegroundColor Cyan

Write-Host "`n2. 由于PostgreSQL访问权限问题，我们采用以下解决方案:" -ForegroundColor Yellow
Write-Host "   A. 手动修复迁移历史（推荐）" -ForegroundColor White
Write-Host "   B. 使用本地SQLite数据库进行开发" -ForegroundColor White
Write-Host "   C. 联系数据库管理员修复访问权限" -ForegroundColor White

Write-Host "`n3. 推荐方案A - 手动修复迁移历史:" -ForegroundColor Yellow
Write-Host "   1. 使用PostgreSQL客户端工具（如pgAdmin、DBeaver等）" -ForegroundColor White
Write-Host "   2. 连接到数据库: 47.119.112.235" -ForegroundColor White
Write-Host "   3. 执行以下SQL:" -ForegroundColor White

$fixSql = @"
-- 删除迁移历史表
DROP TABLE IF EXISTS "__EFMigrationsHistory";

-- 重新创建迁移历史表
CREATE TABLE "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

-- 插入所有迁移记录
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES
('20250729071615_InitCreate', '8.0.0'),
('20250729082144_InitSecond', '8.0.0'),
('20250801073335_InitialCreate', '8.0.0'),
('20250802060605_InitThird', '8.0.0');

-- 检查数据
SELECT 'AppUsers' as table_name, COUNT(*) as record_count FROM "AppUsers"
UNION ALL
SELECT 'AppRoles' as table_name, COUNT(*) as record_count FROM "AppRoles"
UNION ALL
SELECT 'AppPermissions' as table_name, COUNT(*) as record_count FROM "AppPermissions";
"@

Write-Host $fixSql -ForegroundColor Cyan

Write-Host "`n4. 如果数据为空，执行以下SQL插入测试数据:" -ForegroundColor Yellow

$testDataSql = @"
-- 插入测试用户
INSERT INTO "AppUsers" ("Id", "Username", "PasswordHash", "Email", "Phone", "FirstName", "LastName", "LastLoginAt", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'admin',
    '$2a$11$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
    'admin@example.com',
    '+1234567890',
    'Admin',
    'User',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入测试角色
INSERT INTO "AppRoles" ("Id", "Name", "Description", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'Administrator',
    'System Administrator',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;

-- 插入测试权限
INSERT INTO "AppPermissions" ("Id", "Code", "Name", "Description", "CreatedAt", "UpdatedAt", "IsDeleted", "CreateBy", "UpdateBy", "RowVersion")
VALUES (
    '11111111-1111-1111-1111-111111111111',
    'view_dashboard',
    'View Dashboard',
    'Permission to view the dashboard',
    '2025-07-31 06:34:05+00',
    '2025-07-31 06:34:05+00',
    false,
    'system',
    'system',
    '\x'
) ON CONFLICT ("Id") DO NOTHING;
"@

Write-Host $testDataSql -ForegroundColor Cyan

Write-Host "`n5. 修复完成后，测试API功能:" -ForegroundColor Yellow
Write-Host "   powershell -ExecutionPolicy Bypass -File scripts/test-fix.ps1" -ForegroundColor White

Write-Host "`n=== 修复指南完成 ===" -ForegroundColor Green 